package com.asp.aspchain.transaction.elastic;


import com.asp.aspchain.core.dao.AppBaseMapper;
import com.asp.aspchain.core.dao.NotifyRecordMapper;
import com.asp.aspchain.core.dao.TransactionMapper;
import com.asp.aspchain.core.model.AppBaseModel;
import com.asp.aspchain.core.model.NotifyRecordModel;
import com.asp.aspchain.core.model.TransactionModel;
import com.asp.aspchain.transaction.constants.ErrorCode;
import com.asp.aspchain.transaction.disruptor.ListenDataIntoPublishHandler;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.dangdang.ddframe.job.api.ShardingContext;
import com.dangdang.ddframe.job.api.simple.SimpleJob;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
@Component
public class NotifyJob implements SimpleJob {
	@Autowired
	ListenDataIntoPublishHandler listenDataIntoPublishHandler;
	@Autowired
	AppBaseMapper appBaseMapper;
	@Autowired
	TransactionMapper transactionMapper;
	@Autowired
	NotifyRecordMapper notifyRecordMapper;

	@Override
	public void execute(ShardingContext shardingContext) {
		System.out.println("Notify" + String.format("------Thread ID: %s, 任务总片数: %s, " +
						"当前分片项: %s.当前参数: %s," +
						"当前任务名称: %s.当前任务参数: %s",
				Thread.currentThread().getId(),
				shardingContext.getShardingTotalCount(),
				shardingContext.getShardingItem(),
				shardingContext.getShardingParameter(),
				shardingContext.getJobName(),
				shardingContext.getJobParameter()
		));
		List<NotifyRecordModel> NotifyList = notifyRecordMapper.selectList(new EntityWrapper<NotifyRecordModel>().eq("count", shardingContext.getShardingParameter()).where("status!=00"));
		if (NotifyList == null || NotifyList.size() == 0) {
			return;
		}
		for (NotifyRecordModel model : NotifyList) {
			//计算时间是否符合发送要求
			long lastTime = model.getUpdatetime().getTime();
			long now = new Date().getTime();
			int i = Integer.parseInt(shardingContext.getShardingParameter());
			double pow = Math.pow(i, 5);
			Long interval = new Double(pow).longValue() * 60 * 1000;
			if (now - lastTime < interval) {
				continue;
			}
			AppBaseModel appBaseModel = new AppBaseModel();
			appBaseModel.setAppmark(model.getAppmark());
			appBaseModel = appBaseMapper.selectOne(appBaseModel);

			String result = listenDataIntoPublishHandler.sendMessage(model, appBaseModel.getNotifyurl());

			NotifyRecordModel notifyRecord = new NotifyRecordModel();
			notifyRecord.setNotifyid(model.getNotifyid());
			if (ErrorCode.SUCCESS.equals(result)) {
				notifyRecord.setStatus("00");
			} else if (ErrorCode.NOTIFY_FAIL.equals(result)) {
				//对端返回错误
				notifyRecord.setStatus("02");
			} else {
				//网络超时
				notifyRecord.setStatus("01");
			}
			notifyRecord.setCount(Integer.parseInt(shardingContext.getShardingParameter()) + 1);
			notifyRecordMapper.updateById(notifyRecord);

			//更新通知时间
			TransactionModel transactionModel = new TransactionModel();
			transactionModel.setNotifytime(new Date());
			transactionMapper.update(transactionModel, new EntityWrapper<TransactionModel>().eq("notifyid", notifyRecord.getNotifyid()));

		}

	}
}